/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hancadso.client.presenter.grid;

import br.com.sales.generics.client.components.scroll.HScroll;
import static br.com.sales.hancadso.client.presenter.consts.HanCadSOConsts.HEADER_ROW_INDEX;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author v
 */
public class SubItemRequestPopUp extends PopupPanel {

    private final FlexTable table = new FlexTable();
    private final HScroll scroll = new HScroll();

    public SubItemRequestPopUp() {
        super(true);
        createGrid();
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        center();
    }

    @Override
    protected void onUnload() {
        super.onUnload();
        table.clear();
    }

    private void createGrid() {
        setGlassEnabled(true);
        setAnimationEnabled(true);
        table.insertRow(HEADER_ROW_INDEX);
        table.getRowFormatter().addStyleName(HEADER_ROW_INDEX, "flextable-header");

        addColumn("Produto");
        addColumn("Quantidade");

        table.setCellSpacing(0);
        table.addStyleName("flexttable-itens");
        table.getColumnFormatter().setWidth(0, "80%");
        addStyleName("container-popup-itens");
        scroll.add(table);
        this.add(scroll);
    }

    public void addValueTogrid(JSONObject record) {
        int column = 0;
        int index = 1;
        for (String item : record.keySet()) {
            addRow(convert(record.get(item).isObject().get("DS_PRODUTO")), index, column++);
            addRow(convert(record.get(item).isObject().get("QT_QUANTIDADE")), index, column++);
            index++;
            column = 0;
        }
    }

    private Object convert(JSONValue value) {
        if (value == null) {
            return "";
        }
        if (value instanceof JSONString) {
            return value.isString().stringValue();
        } else if (value instanceof JSONNumber) {
            return value.isNumber().toString();
        }
        return "";
    }

    private void addRow(Object value, int row, int cell) {
        Widget widget = createCellWidget(value);
        table.setWidget(row, cell, widget);
        table.getCellFormatter().addStyleName(row, cell, "flextable-cell");
    }

    private Widget createCellWidget(Object cellObject) {
        Widget widget;

        if (cellObject instanceof Widget) {
            widget = (Widget) cellObject;
        } else {
            widget = new Label(cellObject.toString());
        }
        return widget;
    }

    private void addColumn(Object columnHeading) {
        Widget widget = createCellWidget(columnHeading);
        int cell = table.getCellCount(HEADER_ROW_INDEX);

        widget.addStyleName("flextable-column-label");

        table.setWidget(HEADER_ROW_INDEX, cell, widget);

        table.getCellFormatter().addStyleName(HEADER_ROW_INDEX, cell, "flextable-column-label-cell");
    }
}
